
#ifndef  BTREEPLUSSTRUCTS_H
#define  BTREEPLUSSTRUCTS_H

#include "common.h"

struct BtreePlusNode {
    uint16_t pageType;   // 2B 0x0000 -> leaf, 0x0001 -> interior
    uint16_t used;      // 2B
    PageNumberType parent;  //4B
};

struct Data {
    int32_t dataLen;                // 4B bytes of data
    union {
        char raw[12];              //12B
        struct {
            PageNumberType dataPage;        // 4B pageNo of Data
            OffsetInPageType offsetInPage;  // 2B offset in Page
        } ptr;
    } content;
};
struct BtreePlusLeafCell {
    RowIDType key;                  // 8B
    struct Data data;               // 10B
};
struct BtreePlusLeafNode {
    struct BtreePlusNode meta;       //8B
    PageNumberType next;    //4B
    struct BtreePlusLeafCell cells[0];     //
};



struct BtreePlusInteriorCell {
    PageNumberType child;   // 4B pageNo of Data
    RowIDType key;              // 8B
};
struct BtreePlusInteriorNode {
    struct BtreePlusNode meta;               //8B
    struct BtreePlusInteriorCell cells[0];    //
};


template<typename NodeType, typename CellType>
static uint32_t degree(uint32_t pageSize){
    uint32_t full = (pageSize - sizeof(NodeType))/sizeof(CellType);
    return (full%2 != 0) ? (full-1)/2 : full/2;
}

#define leafDegreeOfBtreePlus(pageSize) ::degree<BtreePlusNode,BtreePlusLeafCell>(pageSize)
#define interiorDegreeOfBtreePlus(pageSize) ::degree<BtreePlusNode,BtreePlusInteriorCell>(pageSize)


struct BtreePlusStructs {
    static void test();
};

#endif   /* BTREEPLUSSTRUCTS_H */
